package com.hq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bean.Shouhuo;
import com.hq.bean.ShouhuoMx;
import com.hq.bean.User;
import com.hq.service.CkService;
import com.hq.util.FileUploadUtil;
import com.hq.util.SnowflakeIdUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class CkController {
    private String snow="";
    @Resource
    CkService ckService;

    @RequestMapping("/login.do")
    public String login(User user, HttpSession session){
        User user1 = ckService.login(user);
        if(user1!=null){
            session.setAttribute("user",user1);
            return "redirect:/list.do";
        }else{
            return "redirect:index.jsp";
        }
    }
    @RequestMapping("/list.do")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum, Model model,
                       String snowid,
                       String kuqu,
                       String wid,
                       String pricex,
                       String priced,
                       String numx,
                       String numd,
                       String sumpricex,
                       String sumpriced,
                       String order,
                       String orderby){
        Page<Object> page = PageHelper.startPage(pageNum, 3);
        HashMap map = new HashMap();
        map.put("snowid",snowid);
        map.put("kuqu",kuqu);
        map.put("wid",wid);
        map.put("pricex",pricex);
        map.put("priced",priced);
        map.put("numx",numx);
        map.put("numd",numd);
        map.put("sumpricex",sumpricex);
        map.put("sumpriced",sumpriced);
        map.put("order",order);
        map.put("orderby",orderby);
        List list=ckService.list(map);
        model.addAttribute("list",list);
        model.addAttribute("page",page);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("/cla.do")
    @ResponseBody
    public Object cla(){
        return ckService.cla();
    }

    @RequestMapping("/toshouhuo.do")
    public String toshouhuo(Integer uid,String snow){
        return "shouhuo";
    }
    @RequestMapping("/tochuku.do")
    public String tochuku(Integer uid,String snow){
        return "chuku";
    }

    @RequestMapping("/getsnow.do")
    @ResponseBody
    public String getsnow(){
        if("".equals(snow)||ckService.listsnow(snow)>0){
            long l = new SnowflakeIdUtils(1, 1).nextId();
            snow=l+"";
        }
        return snow;
    }
    @RequestMapping("/findshmx.do")
    @ResponseBody
    public Object findshmx(String snowid1){
        return ckService.findshmx(snowid1);
    }
    @RequestMapping("/addshmx.do")
    public String addshmx(ShouhuoMx shouhuoMx){
        int listsnow = ckService.listsnow(shouhuoMx.getSnow());
        if(listsnow==0){
            ckService.addsh(shouhuoMx.getSnow());
        }
        int i = ckService.addshmx(shouhuoMx);
        if(i>0){
            return "redirect:/toshouhuo.do?snow="+shouhuoMx.getSnow();
        }
        return null;
    }

    @RequestMapping("/updshouhuo.do")
    @ResponseBody
    public int updshouhuo(Shouhuo shouhuo, MultipartFile photo,HttpSession s){
        if(photo.getSize()>0){
            String load = FileUploadUtil.load(s, photo, "upload");
            shouhuo.setShfile(load);
        }
        s.setAttribute("shouhuo",shouhuo);
        System.out.println(
                "=========================================="+shouhuo.toString()
        );
        return ckService.updshouhuo(shouhuo);
    }

    @RequestMapping("/findck.do")
    @ResponseBody
    public Object findck(String snow){
        return ckService.findck(snow);
    }
    @RequestMapping("/delmx.do")
    public String delmx(String snow,String ckname){
        int i = ckService.delmx(snow, ckname);
        if(i>0){
            return "redirect:/tochuku.do?snow="+snow;
        }
        return null;
    }

    @RequestMapping("/tidai.do")
    public String tidai(){
        return "tidai";
    }
    @RequestMapping("/findsnn.do")
    @ResponseBody
    public Object findsnn(String snow){
        return ckService.findsnn(snow);
    }
    @RequestMapping("/findalltidai.do")
    @ResponseBody
    public Object findalltidai(){
        return ckService.findalltidai();
    }
}
